#!/usr/bin/env python
# -*- coding:utf-8 -*-

import sys
import os

from PySide import QtCore, QtGui

import kanboxsettings

from qui.mainWindow import *
from settings import *

class MainWin(QtGui.QMainWindow):
    '''
    Main Window
    '''
    def __init__(self, parent=None, auth2=None):
        super(MainWin, self).__init__(parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        
        self.ui.actionSetting.triggered.connect(self._setting)
        self.ui.actionSynchronize.triggered.connect(self._synchronize)
        self.ui.actionParent_Dir.triggered.connect(self._parentDir)
        
        self.dirModel = QtGui.QFileSystemModel(self)
        self.dirModel.setFilter(QtCore.QDir.AllEntries | QtCore.QDir.NoDotAndDotDot)
        self.ui.tableViewFileList.setModel(self.dirModel)  
        self.ui.tableViewFileList.setShowGrid(False)
        self.ui.tableViewFileList.setRootIndex(self.dirModel.setRootPath(kanboxsettings.Settings['RootPath']))
        self.ui.tableViewFileList.setAlternatingRowColors(True)
        self.ui.tableViewFileList.setStyleSheet(r"gridline-color: rgb(0, 0, 0);\n"
			"border-top-color: rgb(180, 180, 180);\n"
			"alternate-background-color: rgb(207, 236, 255);\n"
			"font: 14pt")
        self.ui.tableViewFileList.clicked.connect(self.ui.tableViewFileList.setRootIndex)
        self.ui.tableViewFileList.clicked.connect(self._synchronize)
                    
        self.auth2 = auth2
        
        self.auth2.build()
        
        self.uInfo = self.auth2.getUserInfo() # Get User Info
        print self.uInfo["status"]
        self.uFileList = self.auth2.getUserFileList() # Get File list
        print self.uFileList["status"]
        
    def _synchronize(self):
        for fInfo in self.uFileList["contents"]:
            print fInfo["fullPath"]
            #QtGui.QListWidgetItem(fInfo["fullPath"], self.ui.listWidget)
            if not fInfo["isFolder"]: # if not a folder
                self.auth2.synUserFile(home=kanboxsettings.Settings['RootPath'], \
                                       path = fInfo["fullPath"])
            else: # if a folder
                path = kanboxsettings.Settings['RootPath']+fInfo["fullPath"]
                if not os.path.exists(path):
                    os.mkdir(kanboxsettings.Settings['RootPath']+fInfo["fullPath"])
                
    def _setting(self):
        win = Settings(self)
        win.setWindowModality(QtCore.Qt.WindowModal)
        win.show()
        
    def _parentDir(self):
        fileinfo = QtCore.QFileInfo(self.dirModel.fileInfo(self.ui.tableViewFileList.currentIndex()))
        filepath = fileinfo.absolutePath()
        print filepath
        if filepath!=kanboxsettings.Settings['RootPath']:        
            preDir = os.path.split(filepath)
            self.ui.tableViewFileList.setRootIndex(self.dirModel.setRootPath(preDir[0]))
            self.ui.tableViewFileList.setCurrentIndex(self.dirModel.setRootPath(preDir[0]))
               
if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    win = MainWin()
    win.show()
    sys.exit(app.exec_()) 
